/******************
 *axios网络请求封装
 *****************/
import axios from 'axios';
import storage from './storage'
import sysConfig from "@/config"
import {ElMessageBox, ElNotification} from 'element-plus'

const VERSION_KEY = import.meta.env.VITE_APP_VERSION_KEY
const VERSION_VAL = import.meta.env.VITE_APP_VERSION_VAL
const DEV = import.meta.env.DEV

axios.defaults.baseURL = import.meta.env.VITE_APP_BASE_URL
axios.defaults.timeout = sysConfig.TIMEOUT
//请求拦截
axios.interceptors.request.use(
    (config) => {
        if (DEV) {
            if (VERSION_VAL) {
                sysConfig.HEADERS[VERSION_KEY] = VERSION_VAL //指定访问服务
            }
        }
        config.headers=sysConfig.HEADERS
        let token = storage.getToken()
        if (token) {
            config.headers[sysConfig.TOKEN_NAME] = token
        }
        return config
    }, (error) => {
        return Promise.reject(error)
    }
)
//响应拦截
axios.interceptors.response.use(
    (response) => {
        if (response.data.code == 401) {
            ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源，请尝试重新登录后再操作。', '无权限访问', {
                type: 'error',
                closeOnClickModal: false,
                center: true,
                confirmButtonText: '重新登录'
            }).then(() => {
                storage.clearSession();
                router.replace({path: '/login'});
            }).catch(() => {
            })
        }
        return response.data;
    }, (error) => {
        if (error.response) {
            if (error.response.status == 404) {
                ElNotification.error({
                    title: '请求错误',
                    message: "Status:404，正在请求不存在的服务器记录！"
                });
            } else if (error.response.status == 500) {
                ElNotification.error({
                    title: '请求错误',
                    message: error.response.data.message || "Status:500，服务器发生错误！"
                });
            } else {
                ElNotification.error({
                    title: '请求错误',
                    message: error.response.data.message || `Status:${error.response.status}，未知错误！`
                });
            }
        } else {
            ElNotification.error({
                title: '请求错误',
                message: "请求服务器无响应！"
            });
        }
        return Promise.reject(error);
    }
);

const http = {
    GET: 'get',
    PUT: 'put',
    PATCH: 'patch',
    POST: 'post',
    DELETE: 'delete',
    /**
     * 发送请求
     * @param from
     * @param isShowSucc
     * @param isShowFail
     */
    send: function (method = "post", path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return new Promise((resolve, reject) => {
            axios({
                method: method,
                url: path,
                params: method == this.GET ? from : {},
                data: method != this.GET ? from : {},
                ...config
            }).then((res) => {
                if (res.isSuccess) {
                    if(isShowOk){
                        ElNotification.success({title: '请求成功', message: '请求成功'});
                    }
                    resolve(res.body);
                } else {
                    if(isShowFail){
                        ElNotification.error({title: '请求失败', message: res.message});
                    }
                    reject(res.message);
                }
            })
        })
    },
    /** get 请求
     * @param  {接口地址} path
     * @param  {请求参数} from
     * @param  {请求成功是否提示} isShowOk
     * @param  {请求失败是否提示} isShowFail
     * @param  {参数} config
     */
    get: function (path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return this.send(this.GET,path,from,isShowOk,isShowFail,config)
    },
    /** put 请求
     * @param  {接口地址} path
     * @param  {请求参数} from
     * @param  {请求成功是否提示} isShowOk
     * @param  {请求失败是否提示} isShowFail
     * @param  {参数} config
     */
    put: function (path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return this.send(this.PUT,path,from,isShowOk,isShowFail,config)
    },
    /**
     * POST请求
     * @param  {接口地址} path
     * @param  {请求参数} from
     * @param  {请求成功是否提示} isShowOk
     * @param  {请求失败是否提示} isShowFail
     * @param  {参数} config
     * @returns {Promise<unknown>}
     */
    post: function (path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return this.send(this.POST,path,from,isShowOk,isShowFail,config)
    },
    /** patch 请求
     * @param  {接口地址} path
     * @param  {请求参数} from
     * @param  {请求成功是否提示} isShowOk
     * @param  {请求失败是否提示} isShowFail
     * @param  {参数} config
     */
    patch: function (path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return this.send(this.PATCH,path,from,isShowOk,isShowFail,config)
    },
    /** delete 请求
     * @param  {接口地址} path
     * @param  {请求参数} from
     * @param  {请求成功是否提示} isShowOk
     * @param  {请求失败是否提示} isShowFail
     * @param  {参数} config
     */
    delete: function (path = '', from = {}, isShowOk = false, isShowFail = false, config = {}) {
        return this.send(this.DELETE,path,from,isShowOk,isShowFail,config)
    }
}
export default http;
